# coding: utf-8

'''
Application for Server
'''

import tornado.web
import tornado.concurrent
import tornado.log
import tornado.options
import os
from .config import mdb
from .router import urls


def log_func(handler):
    if handler.get_status() < 400:
        log_method = tornado.log.access_log.info
    elif handler.get_status() < 500:
        log_method = tornado.log.access_log.warning
    else:
        log_method = tornado.log.access_log.error
    request_time = 1000.0 * handler.request.request_time()
    log_method('%d %s %s (%s) %s %.2fms',
               handler.get_status(), handler.request.method,
               handler.request.uri, handler.request.remote_ip,
               # handler.request.headers['User-Agent'],
               handler.request.arguments,
               request_time)


class Application(tornado.web.Application):
    def __init__(self, urls):
        settings = {
            'base_url': None,
            # 'template_path': 'templates',
            # 'static_path': os.path.join(os.path.dirname(__file__), '../webapp'),
            # ui_modules': {'Entry': EntryModule},
            # xsrf_cookies': True,
            # 'cookie_secret': '#6.w1,8*h@5!',  # 创建应用程序时secure_cookie的密钥,Tornado支持使用set_secure_cookie和 get_secure_cookie
            # 'login_url': '/user/login',  # 使用tornado.web.authenticated放置于restful方法上，用户未登陆时跳转至此
            'debug': False,
            'gzip': True
            # 'default_handler_class': 'controller.base.NotFoundHandler',
            # 'download': './download',
            # 'thread_pool': tornado.concurrent.futures.ThreadPoolExecutor(4),
            # 'log_function': log_func
        }
        mdb.debug = settings['debug']
        super(Application, self).__init__(urls, **settings)


app_tdp = Application(urls)
